﻿using System.Collections.Generic;
using BLL.Model.Messaging;
using BLL.Model.Repositories;
using BLL.Model.Schema;
using System.Linq;

namespace BLL.ProjectManagement
{
    public class ProjectManager
    {
        private IProjectRepository _dalProjectRepository;
        private IHeadRepository _dalHeadRepository;
        private IRecordRepository _recordRepository;
        private readonly Message _message;

        public ProjectManager(IProjectRepository dalProjectRepository, IHeadRepository dalHeadRepository, IRecordRepository recordRepository)
        {
            _dalProjectRepository = dalProjectRepository;
            _dalHeadRepository = dalHeadRepository;
            _recordRepository = recordRepository;
            _message = new Message();
        }

        public IList<Project> GetProjects()
        {
            IList<Project> projects = _dalProjectRepository.GetAll();
            if (projects == null) return new List<Project>();
            return projects;
        }

        public Project GetDefaultProject()
        {
            //return _dalProjectRepository.GetDefaultProject();
            return new Project();
            //TODO : get default project from Parameter field of database
        }

        public bool AddOrUpdateProject(Project project)
        {
            Project existingProject = _dalProjectRepository.Get(project.Id);

            if (existingProject != null)
            {
                _dalProjectRepository.Update(project);
                _message.MessageText = "Project '" + project.Name + "' successfully updated"; //TODO: Change the string concatenation to string.format
                _message.MessageType = MessageType.Success;
            }
            else
            {
                Project insertedProject = _dalProjectRepository.Insert(project);
                if (insertedProject != null)
                {
                    int cashBookId = _dalHeadRepository.Get("Cash Book").Id;
                    int bankBookId = _dalHeadRepository.Get("Bank Book").Id;
                    AddHeadsToProject(insertedProject.Id, new int[] {cashBookId, bankBookId});
                }
                _message.MessageText = "New project '" + insertedProject.Name + "' successfully created"; //TODO: Change the string concatenation to string.format
                _message.MessageType = MessageType.Success;
            }
            return true; //TODO: change the return statement
        }

        public IList<Head> GetHeads(int projectId)
        {
            return _dalProjectRepository.GetAllHeads(projectId);
        }

        public int RemoveHeadsFromProject(int projectId, int[] headIds)
        {
            int count = 0;
            Project project = _dalProjectRepository.Get(projectId);

            if (project == null)
            {
                _message.MessageText = "Invalid project selected.";
                _message.MessageType = MessageType.Warning;
                return 0;
            }

            string headNames = "";
            Head head;
            foreach (int headId in headIds)
            {
                head = _dalHeadRepository.Get(headId);
                if (head != null)
                {
                    if (_dalProjectRepository.RemoveHeadFromProject(projectId, headId))
                    {
                        headNames += string.IsNullOrWhiteSpace(headNames) ? head.Name : ", " + head.Name;
                        count++;
                    }
                }
            }

            if (count > 0)
            {
                _message.MessageText = "The following heads have been removed from project '" + project.Name + "': " +
                                       headNames + ".";
                _message.MessageType = MessageType.Success;
            }
            else
            {
                _message.MessageText = "No head(s) could not be removed from project '" + project.Name + "'.";
                _message.MessageType = MessageType.Information;
            }
            return count;
        }

        public int AddHeadsToProject(int projectId, int[] headIds)
        {
            int count = 0;
            Project project = _dalProjectRepository.Get(projectId);

            if (project == null)
            {
                _message.MessageText = "Invalid project selected.";
                _message.MessageType = MessageType.Warning;
                return 0;
            }

            string headNames = "";
            Head head;
            foreach (int headId in headIds)
            {
                head = _dalHeadRepository.Get(headId);
                if (head != null)
                {
                    if (_dalProjectRepository.AddHeadToProject(projectId, headId))
                    {
                        headNames += string.IsNullOrWhiteSpace(headNames) ? head.Name : ", " + head.Name;
                        count++;
                    }
                }
            }

            if (count > 0)
            {
                _message.MessageText = "The following heads have been added to project '" + project.Name + "': " +
                                       headNames + ".";
                _message.MessageType = MessageType.Success;
            }
            else
            {
                _message.MessageText = "No head(s) could not be added to project '" + project.Name + "'.";
                _message.MessageType = MessageType.Information;
            }
            return count;
        }

        public bool IsRecordFound(int projectId, int headId)
        {
            return _recordRepository.IsRecordFound(projectId, headId);
        }

        public Message GetLatestMessage()
        {
            return _message;
        }
    }
}


